package managedbean.itinerary;

import exception.ExistException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.ItinerarySessionBean;
import session.stateless.SystemMessagesBean;
import session.stateless.TouristAccountSessionBean;
import util.entityhelper.TouristAccountState;
import util.enumeration.Time24H;
import util.generator.TouristMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class ItineraryProvideTravelInfoManagedBean implements Serializable {
    @EJB
    private TouristAccountSessionBean touristAccountMgr;
    @EJB
    private ItinerarySessionBean itineraryMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private TouristAccountState touristAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    
    // fields passed from session
    private Long touristAccountId;
    private String preferredLocale;
    private String itineraryType;
    
    // other fields from database (+ display)
    private String itineraryName;
    private Date arrivalDate;
    private Time24H arrivalTime;
    private Date departureDate;
    private Time24H departureTime;
    private String timeZone;
    private String accomodationPostalCode;
    private Integer noOfAdult;
    private Integer noOfChildren;
    
    // display only
    private MenuModel touristMainMenuModel;
    private MenuModel touristNavMenuModel;
    
    
    public ItineraryProvideTravelInfoManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        redirect = new PageRedirectorBean();
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            touristAccountId = (Long) ctx.getExternalContext().getSessionMap().get("touristAccountId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountId);
            
            if (ctx.getExternalContext().getSessionMap().containsKey("itineraryType")) {
                itineraryType = (String) ctx.getExternalContext().getSessionMap().get("itineraryType");
                // put back the session IDs taken
                ctx.getExternalContext().getSessionMap().put("itineraryType", itineraryType);
            }
        }
        else {
            // redirect back to login
            redirect.goTouristLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */    
    }
    
    public void initView(PhaseEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        try {
//            touristAccountMgr.setSelectedLocale(preferredLocale);
//            
//            touristAcctState = touristAccountMgr.getTouristAccountByID(touristAccountId);
            
            // main menu 
            TouristMenuGenerator menuGeneratorMain = new TouristMenuGenerator();
            touristMainMenuModel = menuGeneratorMain.generateTouristMainMenu();
            // nav menu
            TouristMenuGenerator menuGeneratorNav = new TouristMenuGenerator();
            touristNavMenuModel = menuGeneratorNav.generateTouristNavMenu();
        }
//        catch (ExistException ex) {
////            System.out.println(ex.getMessage());
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
//        }
        catch (Exception ex) {
            ex.printStackTrace();
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }
    
    public void updateTouristTravelInfo(ActionEvent event) {
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        try {
            touristAccountMgr.setSelectedLocale(preferredLocale);
            try {
                touristAcctState = touristAccountMgr.getTouristAccountByID(touristAccountId);
            }
            catch (ExistException ex) {
//                System.out.println(ex.getMessage());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
            }
            
            // reformat time into calendar for calculating purpose in backend
            Calendar newArrivalTime = new GregorianCalendar();
            newArrivalTime.setTime(arrivalDate);
            newArrivalTime.set(Calendar.HOUR_OF_DAY, arrivalTime.getTime());
            Calendar newDepartureTime = new GregorianCalendar();
            newDepartureTime.setTime(departureDate);
            newDepartureTime.set(Calendar.HOUR_OF_DAY, departureTime.getTime());
            
            Long tripItineraryId = itineraryMgr.createTripItinerary(touristAccountId, itineraryName, newArrivalTime, newDepartureTime, timeZone, accomodationPostalCode, noOfAdult, noOfChildren);
            ctx.getExternalContext().getSessionMap().put("selectedTripItineraryId", tripItineraryId);
            
            // send status message to next page (create page)
            String statusMessage = systemMessages.getSystemMessage("ITINERARY_INFO_SAVED", preferredLocale);            
            ctx.getExternalContext().getSessionMap().put("statusMessageFromItineraryTravelInfoPage", statusMessage);
            
            System.out.println("itineraryType: " + itineraryType);
            // go to next page (create itinerary) depending on component ID (DIY or AUTO)
            if (itineraryType.equalsIgnoreCase("DIY")) {
//                redirect.goItineraryCreateDIY();
                redirect.goItineraryViewDailyItinerariesPage();
                System.out.println("here DIY");
            }
            else
            if (itineraryType.equalsIgnoreCase("AUTO")) {
                redirect.goItineraryCreateAuto();
                System.out.println("here Auto");
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public Long getTouristAccountId() {
        return touristAccountId;
    }

    public void setTouristAccountId(Long touristAccountId) {
        this.touristAccountId = touristAccountId;
    }

    public TouristAccountSessionBean getTouristAccountMgr() {
        return touristAccountMgr;
    }

    public void setTouristAccountMgr(TouristAccountSessionBean touristAccountMgr) {
        this.touristAccountMgr = touristAccountMgr;
    }

    public TouristAccountState getTouristAcctState() {
        return touristAcctState;
    }

    public void setTouristAcctState(TouristAccountState touristAcctState) {
        this.touristAcctState = touristAcctState;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }

    public MenuModel getTouristNavMenuModel() {
        return touristNavMenuModel;
    }

    public void setTouristNavMenuModel(MenuModel touristNavMenuModel) {
        this.touristNavMenuModel = touristNavMenuModel;
    }

    public String getAccomodationPostalCode() {
        return accomodationPostalCode;
    }

    public void setAccomodationPostalCode(String accomodationPostalCode) {
        this.accomodationPostalCode = accomodationPostalCode;
    }

    public Date getArrivalDate() {
        return arrivalDate;
    }

    public void setArrivalDate(Date arrivalDate) {
        this.arrivalDate = arrivalDate;
    }

    public Date getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(Date departureDate) {
        this.departureDate = departureDate;
    }

    public Integer getNoOfAdult() {
        return noOfAdult;
    }

    public void setNoOfAdult(Integer noOfAdult) {
        this.noOfAdult = noOfAdult;
    }

    public Integer getNoOfChildren() {
        return noOfChildren;
    }

    public void setNoOfChildren(Integer noOfChildren) {
        this.noOfChildren = noOfChildren;
    }

    public Time24H getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(Time24H arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public Time24H getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Time24H departureTime) {
        this.departureTime = departureTime;
    }

    public ItinerarySessionBean getItineraryMgr() {
        return itineraryMgr;
    }

    public void setItineraryMgr(ItinerarySessionBean itineraryMgr) {
        this.itineraryMgr = itineraryMgr;
    }

    public String getItineraryType() {
        return itineraryType;
    }

    public void setItineraryType(String itineraryType) {
        this.itineraryType = itineraryType;
    }

    public String getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(String timeZone) {
        this.timeZone = timeZone;
    }

    public String getItineraryName() {
        return itineraryName;
    }

    public void setItineraryName(String itineraryName) {
        this.itineraryName = itineraryName;
    }
}